#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/input.h>

#include "TpDetec.h"
static struct input_event myevt; //输入设备 数据结构体

static int fd = 0; //触摸屏设备的文件描述符
int rerr = 0;

int old_x = 0;
int new_x = 0;
int new_y = 0;	

int valid = 0;
int pressed = -1;

int flag_left = 0;
int flag_right = 0;

void Tp_Init(void)
{
	struct input_absinfo myabsinfo = {0};//输入设备 特征结构体
 
    //0. open the file
    //open (const char *__path, int __oflag, ...)
    fd = open(TP_DEV_PATH, O_RDONLY);
    if(fd < 0)
    {
        perror("Open file failed!\n");
		exit(-1);
	}

	//through ioctl, find max slot value
	if (ioctl(fd, EVIOCGABS(ABS_MT_SLOT), &myabsinfo) < 0)
	{
		perror("ioctl failed.");
		exit(-1);
	}
}



void* Tp_Detect(void *arg)
{
	//a counter
	int cnt_x = 0;
	int delta = 0;

	Tp_Init();
    while(1)
    {
		rerr = read(fd, &myevt, sizeof(myevt));
		if(rerr > 0)
		{
			switch (myevt.type)
			{ 
				case EV_KEY: 
					    pressed = myevt.value;
						//valid = 1;
						break;

				case EV_ABS: 
					if(myevt.code == ABS_X)
					{
							new_x = myevt.value;
							//valid = 1;
					}
					else if(myevt.code  == ABS_Y)
					{
							new_y = myevt.value;
							//valid = 1;
					}
					break;
			
				case EV_SYN:	
					if((myevt.code == SYN_REPORT))
					{
						switch(pressed)
						{
							case 0://released
									printf("Realeased\n");
									valid = 1;
									if(cnt_x >= 5)
									{
										delta = new_x-old_x;
										if(delta > 20)
										{
											puts("right---->");
											flag_right = 1;
										}
										else if(delta < -20)
										{
											puts("<-------left");
											flag_left = 1;
										}
										delta = 0;
										cnt_x = 0;
								   }
									break;
							case 1://pressed 
								   printf("Pressed x=%d, y=%d\n", new_x, new_y);
								   break;
							case -1://dragged
								   printf("Dragged x=%d, y=%d\n", new_x, new_y);
								   if(cnt_x == 0)  
								   {
										old_x = new_x;  
								   }
								   cnt_x++;        
								   break;
						}
						pressed = -1;
					}
					break;
			}
		}
		else 
		{
			printf("Read keyvalue failed!\n");
		}
    }
    //2. close the file
    rerr = close(fd);
    if(rerr < 0)
    {
        printf("Close file failed!\n");
        return (void *)-1;
    }

    return (void*)0;
}


/**
 * @brief 识别单块区域是否被点击
 * 
 * @param x_touch 点击坐标的x
 * @param y_touch 点击坐标的y
 * @param x_start 待识别区域起始坐标的x
 * @param y_start 待识别区域起始坐标的y
 * @param x_end   待识别区域结束坐标的x
 * @param y_end   待识别区域结束坐标的y
 * @return 此块区域被点击BETOUCH 此块区域未被点击NOTTOUCH
 */
int check_region_touched(int x_touch,int y_touch,int x_start,int y_start,int x_end,int y_end)
{
	//输入参数合法性检查
    if(x_start>x_end)
    {
        printf("you input x_start > x_end\n");
        return NOTTOUCH;
    }else if(y_start>y_end){
        printf("you input y_start > y_end\n");
        return NOTTOUCH;
    }
	

    if((x_touch>=x_start && x_touch<=x_end) && (y_touch>=y_start && y_touch<=y_end))
    {
        return BETOUCH;
    }
	else
	{
		return NOTTOUCH;
	}
}
